#include "frame_camera.h"
#include "ui_frame_camera.h"
#include <QMessageBox>

Frame_Camera::Frame_Camera(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::Frame_Camera)
{
    ui->setupUi(this);
    this->setObjectName("mainWindow");
    this->setWindowTitle("离心机摄像头观察窗口");
    //this->setWindowFlags(Qt::FramelessWindowHint);
    loadCamera();
}

Frame_Camera::~Frame_Camera()
{
    delete ui;
    if(nullptr != camera){
        delete camera;
        camera = nullptr;
    }
    qDebug("~Frame_Camera");
}

void Frame_Camera::showUi()
{
    camera->resume();
    show();
}

void Frame_Camera::loadCamera()
{
    camera = new ImageCamera("/dev/video0", this);
    camera->setImageSize(640, 480);
    connect(camera, SIGNAL(image(QImage)), this, SLOT(showCameraImg(QImage)), Qt::QueuedConnection);
    if(!camera->initCamera()){
        QMessageBox::information(this, QString(), camera->errorString());
        return;
    }
    if(!camera->startCapture()){
        QMessageBox::information(this, QString(), camera->errorString());
        return;
    }
}

void Frame_Camera::showCameraImg(QImage img)
{
    ui->labe_camera->setPixmap(QPixmap::fromImage(img));
}

void Frame_Camera::on_pushButton_resume_clicked()
{
    camera->resume();
}

void Frame_Camera::on_pushButton_pause_clicked()
{
    camera->pause();
}

void Frame_Camera::on_pushButton_close_clicked()
{
    hide();
    camera->pause();
}

void Frame_Camera::mousePressEvent(QMouseEvent *event)
{
    dragPosition = event->globalPos()-frameGeometry().topLeft();
}

void Frame_Camera::mouseMoveEvent(QMouseEvent *event)
{
    move(event->globalPos() - dragPosition);
}
